using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Systepic.Projects;

public partial class Admin_Controls_AddProject : System.Web.UI.UserControl
{
	public event EventHandler ProjectAdded;

	protected void Page_Load(object sender, EventArgs e)
	{
	}

	protected void btnAddProject_Click(object sender, EventArgs e)
	{
		string name = null;
		string description = 
			(this.txtDesc.Text != null) ? this.txtDesc.Text : "";
		DateTime startDate;
		DateTime completionDate;
		DateTime creationDate = DateTime.Now;

		// Validation
		if(this.txtName.Text == null)
		{
			this.lblStatus.Text = "Please enter a name";
			return;
		} else name = this.txtName.Text;

		Project p = new Project();
		p.Name = this.txtName.Text;
		p.Description = description;

			if (DateTime.TryParse(this.txtStartDate.Text, out startDate))
				p.StartDate = startDate;
			else
				p.StartDate = DateTime.MinValue;

			if (DateTime.TryParse(this.txtCompletionDate.Text, out completionDate))
				p.CompletionDate = completionDate;
			else
				p.CompletionDate = DateTime.MinValue;

		p.CreationDate = creationDate;

		if(ProjectsHelper.AddProject(p))
		{
			this.lblStatus.Text = "Project Added";
			this.txtName.Text = "";
			this.txtStartDate.Text = "";
			this.txtCompletionDate.Text = "";
			this.txtDesc.Text = "";
		} 
		else
		{
			this.lblStatus.Text = "Project Failed to add";
		}

		if (this.ProjectAdded != null)
			this.ProjectAdded(this, new EventArgs());
	}
}
